<?php
	
	class Yahoo_Status extends WP_Widget {
 
    //Khởi tạo contructor của 1 lớp
    function Yahoo_Status(){
        parent::WP_Widget('Yahoo_Status_Widget', 
            'Yahoo Status', 
            array('description' => 'Trạng thái nick yahoo.'));
    }
	function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters( 'widget_title', 
            empty($instance['title']) ? '' : $instance['title'], 
            $instance, $this->id_base);
        $text = apply_filters( 'widget_text', 
            $instance['text'], $instance );
        echo $before_widget;
        if ( !empty( $title ) ) { 
            echo $before_title . $title . $after_title; } ?>         
            <ul class="nick_status">
            <?php $str = explode(",", $text);                
                foreach ($str as $s) :
						list($nick , $dept) = explode(":", $s);
					?>
                  	
                  <li>
                    <a href="ymsgr:sendIM?<?php echo trim($nick);?>">
                        <img border="0" src="http://mail.opi.yahoo.com/online?u=<?php echo trim($nick);?>&m=g&t=1&l=us" />&nbsp; <?= trim($dept)?>
                    </a>
                  </li>
            <?php endforeach;                        
            ?>
            </ul>         
        <?php
        echo $after_widget;
    }
	
	function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    if ( current_user_can('unfiltered_html') )
        $instance['text'] =  $new_instance['text'];
    else
        $instance['text'] = stripslashes( 
            wp_filter_post_kses( addslashes($new_instance['text']) ) 
        );      
    return $instance;
	}
	
	function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, 
            array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
        $text = format_to_edit($instance['text']);
?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php _e('Tiêu đề:'); ?> </label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
                name="<?php echo $this->get_field_name('title'); ?>" type="text"
                value="<?php echo  esc_attr($title);?>" />
        </p>
            <label for="<?php echo $this->get_field_id('text'); ?>">
                <?php _e('Nick yahoo cách nhau bởi dấu ","'); ?> </label>
            <textarea class="widefat" rows="5" cols="10"
                id="<?php echo $this->get_field_id('text'); ?>"
                name="<?php echo $this->get_field_name('text'); ?>">
                    <?php echo $text;?>
            </textarea>
		  <p>
            <label for="<?php echo $this->get_field_id('text'); ?>">
                <?php _e('Ví dụ: nickname1:bophan1, nickname2:bophan2'); ?>
            </label>          
        </p>
<?php
    }
}
     
register_widget('Yahoo_Status');

/*****
	WIDGET BẢN TIN
*****/
class BanTin extends WP_Widget {
 
    //Khởi tạo contructor của 1 lớp
    function BanTin()
	{
        parent::WP_Widget('BanTin', 'zBản tin', array('description' => 'Hiện tin tức gần đây'));
    }
	function widget($args, $instance)
	{
		extract($args);
		$title = apply_filters('widget_title', empty($instance['title']) ? 'Recent From ' : $instance['title']);
		$posts_number = empty($instance['posts_number']) ? '' : (int) $instance['posts_number'];
		$blog_category = empty($instance['blog_category']) ? '' : (int) $instance['blog_category'];
	 
		echo $before_widget;
	 
		if ( $title )
		echo $before_title . $title . $after_title;
	?>
		<ul>
		<?php query_posts("showposts=".$posts_number."&cat=".$blog_category);
		
		if (have_posts()) : while (have_posts()) : the_post(); ?>
			
				<li><a href="<?php the_permalink(); ?>"><?php the_title() ?>&hellip;</a></li>
		
		<?php endwhile; endif; wp_reset_query(); ?>
	 	</ul>
	<?php
		echo $after_widget;
	}

	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		$instance['title'] = stripslashes($new_instance['title']);
		$instance['posts_number'] = (int) $new_instance['posts_number'];
		$instance['blog_category'] = (int) $new_instance['blog_category'];
		return $instance;
	}
	
	function form($instance)
	{
		//Defaults
		$instance = wp_parse_args( (array) $instance, array('title'=>'Tiêu đề Categories', 'posts_number'=>'5', 'blog_category'=>'') );
	 
		$title = esc_attr($instance['title']);
		$posts_number = (int) $instance['posts_number'];
		$blog_category = (int) $instance['blog_category'];
	 
		# Title
		echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title:' . '</label><input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></p>';
		# Number Of Posts
		echo '<p><label for="' . $this->get_field_id('posts_number') . '">' . 'Số lượng hiển thị:' . '</label><input class="widefat" id="' . $this->get_field_id('posts_number') . '" name="' . $this->get_field_name('posts_number') . '" type="text" value="' . $posts_number . '" /></p>';
		# Category ?>
		<?php 
			$cats_array = get_categories('hide_empty=0');
		?>
		<p>
			<label for="<?php echo $this->get_field_id('blog_category'); ?>">Category</label>
			<select name="<?php echo $this->get_field_name('blog_category'); ?>" id="<?php echo $this->get_field_id('blog_category'); ?>" class="widefat">
				<?php foreach( $cats_array as $category ) { ?>
					<option value="<?php echo $category->cat_ID; ?>"<?php selected( $instance['blog_category'], $category->cat_ID ); ?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p> 
		<?php
	}
}
     
register_widget('BanTin');
/*****
	END WIDGET BẢN TIN
*****/

/***
	WIDGET SẢN PHẨM KHUYẾN MÃI
***/
	
class SP_KhuyenMai extends WP_Widget {
 
    //Khởi tạo contructor của 1 lớp
    function SP_KhuyenMai(){
        parent::WP_Widget('SP_KhuyenMai', 
            'zSP Khuyen Mai', 
            array('description' => 'Hiển thị ản phẩm khuyến mãi'));
    }
	function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters( 'widget_title', 
            empty($instance['title']) ? '' : $instance['title'], 
            $instance, $this->id_base);
        $text = apply_filters( 'widget_text', 
            $instance['text'], $instance );
        echo $before_widget;
        if ( !empty( $title ) ) { 
            echo $before_title . $title . $after_title; } ?>         
            <ul class="widget_sp_khuyenmai">
        <?
			$arg = array('post_type'  => 'san-pham',
						 'meta_key'   =>  'sanpham_khuyenmai',
						 'meta_value' => 'on' );
			$wp_query = new WP_Query($arg);
					
			if($wp_query->have_posts())
			{
				while($wp_query->have_posts())
				{
					$wp_query->the_post();
					
					if(get_post_meta(get_the_ID(),'thumb_image',true) != '')
					{
		?>			
			<li><a href="<? the_permalink()?>" title="<? the_title()?>"><img src="<?= get_post_meta(get_the_ID(),'thumb_image',true) ?>" title="<? the_title()?>" alt="<? the_title()?>" /></a>
            	<p class="des_img"><? the_title() ?><br />
                <span class="discount"><?= format_number(get_post_meta(get_the_ID(),'giaban',true),0).' VND' ?></span><br />
  				<span class="price"><?
				$gia = (int)get_post_meta(get_the_ID(),'giaban',true) - (int)get_post_meta(get_the_ID(),'giamgia',true);
				echo format_number($gia,0).' VND'; ?></span></p>
                
            </li>
		<?
					}
				}
			}
		?>
            </ul>         
        <?php
        echo $after_widget;
    }
	
	function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance['title'] = stripslashes($new_instance['title']);
    return $instance;
	}
	
	function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, 
            array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php _e('Tiêu đề:'); ?> </label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
                name="<?php echo $this->get_field_name('title'); ?>" type="text"
                value="<?php echo  esc_attr($title);?>" />
        </p>            
<?php
    }
}
     
register_widget('SP_KhuyenMai');
	
/***
	END WIDGET SẢN PHẨM KHUYẾN MÃI
***/

?>
